package com.yvan.codeslave.batch.process;

import com.google.common.base.Strings;
import com.yvan.codeslave.batch.Consts;
import com.yvan.platform.FileUtils;
import com.yvan.platform.JsonWapper;
import lombok.val;
import lombok.var;

public class FileProcess {
    public static void process(JsonWapper jw) {
        val rootBasePath = jw.asStr(Consts.FILE_GENERATOR, Consts.FILE_BASE_PATH);

        for (val jwFile : jw.asJsonWapperList(Consts.FILE_GENERATOR, Consts.File)) {
            var basePath = jwFile.asStr(Consts.FILE_BASE_PATH);
            var forTable = jwFile.asStr(Consts.FILE_FOR_TABLE);
            var disable = jwFile.asBoolean(Consts.FILE_DISABLE);
            var forAction = jwFile.asStr(Consts.FILE_FOR_ACTION);
            val template = jwFile.asStr(Consts.FILE_TEMPLATE);
            val fileName = jwFile.asStr(Consts.FILE_FILE_NAME);

            val fileKey = FileUtils.normalizePath(rootBasePath, basePath, fileName);
            jw.asDic(Consts.File, fileKey)
                    .set(Consts.JAVA_DISABLE, disable)
                    .set(Consts.JAVA_TEMPLATE, template);

            if (!Strings.isNullOrEmpty(forTable)) {
                jw.asDic(Consts.File, fileKey)
                        .set(Consts.JAVA_FOR_TABLE, forTable);

                if (!Strings.isNullOrEmpty(forAction)) {
                    jw.asDic(Consts.File, fileKey)
                            .set(Consts.JAVA_FOR_ACTION, forAction);
                }
            }
        }

        jw.remove(Consts.FILE_GENERATOR);
    }
}
